package com.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bean.CourseListBean;
import com.xiaochun.hxhj.R;

import java.util.List;

/**
 * Created by fei on 2017/12/30.
 */

public class MyVourseRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int TYPE_BANNER = 0;//头部轮播图
    public static final int TYPE_RECOMMEND = 1;//往期课程
    public static final int TYPE_NORMAL = 5;
    public static final int TYPE_BOTTOM = 6;//底部

    private List<CourseListBean.DataBeanX.AgoBean.DataBean> mDatas;
    private Context context;

    public MyVourseRecyclerViewAdapter(List<CourseListBean.DataBeanX.AgoBean.DataBean> mDatas, Context context) {
        this.mDatas = mDatas;
        this.context = context;
    }

    private View bannerView;
    private View bottomView;
    private View recommendView;

    private OnItemClickListener mListener;

    public void setOnItemClickListener(OnItemClickListener li) {
        mListener = li;
    }

    public void setBannerView(View bannerView) {
        this.bannerView = bannerView;
    }
    public void setRecommendView(View recommendView) {
        this.recommendView = recommendView;
    }


    public void setBottomView(View bottomView) {
        this.bottomView = bottomView;
    }

    public void setDatas(List<CourseListBean.DataBeanX.AgoBean.DataBean> datas) {
        this.mDatas = datas;
        notifyDataSetChanged();
        /*if (datas.size() > 10){
            notifyItemRangeChanged(datas.size()-10,10);
        }else{
            notifyItemRangeChanged(0,datas.size());
        }*/
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) return TYPE_BANNER;
        if (position == 1) return TYPE_RECOMMEND;
        if (position == mDatas.size() + 2) return TYPE_BOTTOM;
        return TYPE_NORMAL;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (bannerView != null && viewType == TYPE_BANNER) {
            return new Holder(bannerView);
        }
        if (recommendView != null && viewType == TYPE_RECOMMEND) {
            return new Holder(recommendView);
        }
        if (bottomView != null && viewType == TYPE_BOTTOM) {
            return new Holder(bottomView);

        }
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_course_item, parent, false);
        return new Holder(layout);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
        if (getItemViewType(position) == TYPE_BANNER) {
            return;
        }
        if (getItemViewType(position) == TYPE_RECOMMEND) {
            return;
        }
        if (getItemViewType(position) == TYPE_BOTTOM) {
            return;
        }
        final int pos = getRealPosition(viewHolder);
        final CourseListBean.DataBeanX.AgoBean.DataBean bean = mDatas.get(pos);
        if (viewHolder instanceof Holder) {

            ((Holder) viewHolder).tv_course_name.setText(bean.getTitle());
            ((Holder) viewHolder).tv_course_content.setText(bean.getName());
            ((Holder) viewHolder).tv_study_num.setText("学习了"+bean.getNum()+"遍");
            int num = bean.getStart();
            ((Holder) viewHolder).ll_star.removeAllViews();
            for (int i = 0;i<num;i++){
                View view = View.inflate(context,R.layout.item_star,null);
                ((Holder) viewHolder).ll_star.addView(view);
            }


            if (mListener != null) {
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mListener.onItemClick(pos);
                    }
                });
            }


        }
    }





    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return position - 2;
    }

    @Override
    public int getItemCount() {
        return mDatas.size() + 3;
    }

    class Holder extends RecyclerView.ViewHolder {

        private TextView tv_course_name;//名字
        private TextView tv_course_content;//介绍
        private TextView tv_study_num;//查看详情
        private LinearLayout ll_star;//查看详情

        public Holder(View itemView) {
            super(itemView);
            tv_course_name = (TextView) itemView.findViewById(R.id.tv_course_name);
            tv_course_content = (TextView) itemView.findViewById(R.id.tv_course_content);
            tv_study_num = (TextView) itemView.findViewById(R.id.tv_study_num);
            ll_star = (LinearLayout) itemView.findViewById(R.id.ll_star);

        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }



}